package com.kylin.common.paginate;

import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;
import org.hibernate.Query;

public class PaginatedListHibernate implements PaginatedList,Page {
	public static final int DEFAULT_PAGE_SIZE = 10;

	private List list;

	private int pageNumber;

	private int objectsPerPage = DEFAULT_PAGE_SIZE;

	private int fullListSize;

	private SortOrderEnum sortDirection = SortOrderEnum.ASCENDING;

	private String sortCriterion = null;

	private String searchId = null;

	public PaginatedListHibernate() {
	}

	public PaginatedListHibernate(Query queryCount, Query query,
			int pageNumber, int objectsPerPage) {
		buildPage(queryCount, query, pageNumber, objectsPerPage);

	}

	public PaginatedListHibernate(Query queryCount, Query query, int pageNumber) {
		buildPage(queryCount, query, pageNumber, DEFAULT_PAGE_SIZE);
	}

	private void buildPage(Query queryCount, Query query, int pageNumber,
			int objectsPerPage) {
		this.pageNumber = pageNumber;
		this.objectsPerPage = objectsPerPage;

		this.fullListSize = ((Integer) queryCount.uniqueResult()).intValue();
        if (Integer.MAX_VALUE == this.pageNumber || this.pageNumber > getLastPageNumber())   //last page
        {
            this.pageNumber = getLastPageNumber();
        }
		list = query
				.setFirstResult((this.pageNumber - 1) * this.objectsPerPage)
				.setMaxResults(this.objectsPerPage).list();
	}

	public int getFullListSize() {
		return fullListSize;
	}

	public void setFullListSize(int fullListSize) {
		this.fullListSize = fullListSize;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getObjectsPerPage() {
		return objectsPerPage;
	}

	public void setObjectsPerPage(int objectsPerPage) {
		this.objectsPerPage = objectsPerPage;
	}

	public int getPageNumber() {
		return getLastPageNumber()>pageNumber?pageNumber:getLastPageNumber();
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}

	public String getSortCriterion() {
		return sortCriterion;
	}

	public void setSortCriterion(String sortCriterion) {
		this.sortCriterion = sortCriterion;
	}

	public SortOrderEnum getSortDirection() {
		return sortDirection;
	}

	public void setSortDirection(SortOrderEnum sortDirection) {
		this.sortDirection = sortDirection;
	}

	/* 
	 * added by wuxiaohua, to implement the original Page Interface  
	 */
    /**
     * @see com.souchang.highfly.common.Page#getElements()
     */
    public List getElements() {
        return list;
    }
    /**
     * @see com.souchang.highfly.common.Page#getFirstElementNumber()
     */
    public int getFirstElementNumber() {
        return (getPageNumber() - 1) * getPageSize() + 1;        
    }
    /**
     * @see com.souchang.highfly.common.Page#getLastElementNumber()
     */
    public int getLastElementNumber() {
        int fullPage = getFirstElementNumber() + getPageSize() - 1;
        return getTotalElementsCount() < fullPage ? getTotalElementsCount() : fullPage;
    }
    /**
     * @see com.souchang.highfly.common.Page#getLastPageNumber()
     */
    public int getLastPageNumber() {
        return getTotalElementsCount() % getPageSize() == 0 ?
                getTotalElementsCount() / getPageSize() : getTotalElementsCount() / getPageSize() + 1;
        
    }
    /**
     * @see com.souchang.highfly.common.Page#getNextPageNumber()
     */
    public int getNextPageNumber() {
        return getPageNumber() + 1;        
    }
    /**
     * @see com.souchang.highfly.common.Page#getPageSize()
     */
    public int getPageSize() {
        return objectsPerPage;
    }
    /**
     * @see com.souchang.highfly.common.Page#getPreviousPageNumber()
     */
    public int getPreviousPageNumber() {
        return getPageNumber() - 1;
    }
    /**
     * @see com.souchang.highfly.common.Page#getTotalElementsCount()
     */
    public int getTotalElementsCount() {
        return fullListSize;
    }
    /**
     * @see com.souchang.highfly.common.Page#hasNextPage()
     */
    public boolean hasNextPage() {
        return getLastPageNumber() > getPageNumber();        
    }
    /**
     * @see com.souchang.highfly.common.Page#hasPreviousPage()
     */
    public boolean hasPreviousPage() {
        return getPageNumber() > 1;
    }
    /**
     * @see com.souchang.highfly.common.Page#isFirstPage()
     */
    public boolean isFirstPage() {
        return getPageNumber() == 1;
    }
    /**
     * @see com.souchang.highfly.common.Page#isLastPage()
     */
    public boolean isLastPage() {
        return getPageNumber() >= getLastPageNumber();
    }
    /**
     * @see com.souchang.highfly.common.Page#setElements(java.util.List)
     */
    public void setElements(List l) {
        // TODO 待实现的方法
        this.list = l;
    }

	public String toString() {
		// TODO 自动生成方法存根
		return "getFirstElementNumber"+this.getFirstElementNumber()+
		"FullSize"+this.getFullListSize()+
		"PageNumber"+this.getPageNumber()+
		"PageSize"+this.getPageSize();
	}
    
}
